// import {useStore} from 'vuex';
import { createRouter, createWebHashHistory } from 'vue-router';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

const routes = [
  //首页
  {
    path: '/',
    name: 'Index',
    meta: {
      title: 'Bing酒店管理系统',
    },
    component: () => import('../views/Index.vue'),
  },
  //登录页
  {
    path: '/login',
    name: 'Login',
    meta: {
      title: '登录',
    },
    component: () => import('../views/Login.vue'),
  },
  //布局页
  {
    path: '/layout',
    name: 'Layout',
    meta: {
      title: 'Bing酒店管理系统',
    },
    component: () => import('../views/Layout.vue'),
    children: [
      //消息
      {
        path: 'message',
        name: 'Message',
        meta: {
          title: '消息',
        },
        component: () => import('../views/home/Message.vue'),
      },
      //邮件
      {
        path: 'email',
        name: 'Email',
        meta: {
          title: '邮件',
        },
        component: () => import('../views/home/Email.vue'),
      },
      //首页
      {
        path: '',
        name: 'Home',
        meta: {
          title: '首页',
        },
        component: () => import('../views/home/Home.vue'),
      },
      //个人中心
      {
        path: 'mine',
        name: 'Mine',
        meta: {
          title: '个人中心',
        },
        component: () => import('../views/account/Mine.vue'),
      },
      //修改密码
      {
        path: 'changePwd',
        name: 'ChangePwd',
        meta: {
          title: '修改密码',
        },
        component: () => import('../views/account/ChangePwd.vue'),
      },
      //角色管理
      {
        path: 'role',
        name: 'Role',
        meta: {
          title: '角色管理',
          // roles: [1], //系统管理员才能访问
        },
        component: () => import('../views/role/Role.vue'),
      },
      //账户管理
      {
        path: 'account',
        name: 'Account',
        meta: {
          title: '账户管理',
        },
        component: () => import('../views/account/Account.vue'),
      },
      //房间类型
      {
        path: 'roomType',
        name: 'RoomType',
        meta: {
          title: '房间类型',
          // roles: [1], //系统管理员才能访问
        },
        component: () => import('../views/roomType/RoomType.vue'),
      },
      //房间管理
      {
        path: 'room',
        name: 'Room',
        meta: {
          title: '房间管理',
        },
        component: () => import('../views/room/Room.vue'),
      },
      //顾客管理
      {
        path: 'guest',
        name: 'Guest',
        meta: {
          title: '顾客管理',
        },
        component: () => import('../views/guest/Guest.vue'),
      },
    ],
  },
  //404
  {
    path: '/:pathMatch(.*)*',
    name: 'Error404',
    meta: {
      title: '404',
    },
    component: () => import('../views/Error404.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

//前置守卫
router.beforeEach((to, from, next) => {
  NProgress.start();

  const {meta} = to;

  if(meta && meta.roles){
    //路由元信息里配置了角色权限
    //在 setup 钩子函数中调用 useStore
    const $store = useStore();

    console.log('$store:')
    console.log($store)

    const {roleId} = $store.state.admin.adminInfo.role;

    if(meta.roles.includes(roleId)){
      //当前登录账号有访问权限
      next();
    }
  }else{
    //路由元信息里没有配置角色权限
    next();
  }
});

//后置守卫
router.afterEach((to, from) => {
  const {meta} = to;

  if(meta && meta.title){
    //路由元信息里配置了标题
    document.title = to.meta.title;
  }

  NProgress.done();
});

export default router
